13. Colors

Colors Heading

Colors

Adding in colors

Colors in CSS can be specified by the following methods:

  1. Hexadecimal colors
  2. RGB colors
  3. Predefined/Cross-browser color names
  4. RGBA colors
  5. HSL colors
  6. HSLA colors

Let's talk about the first 3 since those are the most common.

Hexadecimal colors

Hexadecimal Colors

A hexadecimal color is specified with: #RRGGBB, where the RR (red), GG (green) and BB (blue) hexadecimal integers specify the components of the color. All values must be between 00 and FF.

For example, the #0000ff value is rendered as blue, because the blue component is set to its highest value (ff) and the others are set to 00.

Example

Define different HEX colors:

#p1 {
  background-color: #ff0000;
}

#p2 {
  background-color: #00ff00;
}

#p3 {
  background-color: #0000ff;
}

RBG colors

RGB Colors

An RGB color value is specified with the rgb() function, which has the following syntax:

rgb(red, green, blue)

Each parameter (red, green, and blue) defines the intensity of the color and can be an integer between 0 and 255 or a percentage value (from 0% to 100%).

For example, the rgb(0,0,255) value is rendered as blue, because the blue parameter is set to its highest value (255) and the others are set to 0.

Also, the following values define equal color: rgb(0,0,255) and rgb(0%,0%,100%).

Example

Define different RGB colors:

#p1 {
  background-color: rgb(255, 0, 0);
}

#p2 {
  background-color: rgb(0, 255, 0);
}

#p3 {
  background-color: rgb(0, 0, 255);
}

Predefined colors

Predefined/Cross-browser Color Names

140 color names are predefined in the HTML and CSS color specification.

There's quite a few of these - check out this list to see more.

Quiz 10 - Color Values

Which of the following values is not valid in CSS when defining a color?

SOLUTION: `color: FFFFFF;`

Quiz 11 - Color Values

Which CSS declarations can be used to represent the standard color for blue?

SOLUTION:
  • `color: blue;`
  • `color: rgb(0, 0, 255);`
  • `color: #00f;`